<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.num" placeholder="流水号" style="width: 150px;" class="filter-item" />
      <el-input v-model="listQuery.num" placeholder="名称" style="width: 150px;" class="filter-item" />
      <el-select v-model="listQuery.proType" placeholder="财务类别" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in proTypes" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-select v-model="listQuery.proType" placeholder="收款方" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in proTypes" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-select v-model="listQuery.proType" placeholder="状态" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in proTypes" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-refresh-left" @click="handleReset">
        重置
      </el-button>
      <el-button class="filter-item fr" type="warning" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table :data="tableData" stripe max-height="600">
      <el-table-column prop="id" label="流水号" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="num" label="分类" />
      <el-table-column prop="standard" label="收款方" />
      <el-table-column prop="price" label="付款金额" />
      <el-table-column prop="count" label="状态" />
      <el-table-column prop="totalMoney" label="创建时间" />
      <el-table-column prop="supply_num" label="付款时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.$index, scope.row)"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handlePay(scope.$index, scope.row)"
          >付款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 新增/编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="采购单号" prop="num">
          <el-input v-model="temp.num" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplyId">
          <el-select v-model="temp.supplyId" class="filter-item" placeholder="请选择供应商">
            <el-option v-for="i in 3" :key="i" :label="i" :value="i" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productId">
          <el-select v-model="temp.productId" class="filter-item" placeholder="请选择产品">
            <el-option v-for="i in 3" :key="i" :label="i" :value="i" />
          </el-select>
        </el-form-item>
        <el-form-item label="采购数量" prop="count">
          <el-input-number v-model="temp.count" :step="1"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { wareAll } from '@/api'
import Pagination from '@/components/Pagination'

export default {
  name: 'Pay',
  components: {
    Pagination
  },
  data() {
    return {
      listQuery: {
        num: '', // 采购单号
        name: '', // 产品名称
        proType: '', // 产品类别
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      proTypes: [
        {
          id: '111',
          name: '电器'
        },
        {
          id: '222',
          name: '服装'
        }
      ],
      tableData: [],
      total: 100,
      temp: {
        num: '', // 采购单号
        supplyId: '', // 供应商id
        productId: '', // 产品id
        count: 0 // 采购数量
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑采购',
        create: '新增采购'
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.inWare()
  },
  methods: {
    // 筛选
    handleFilter() {
      console.log('筛选')
    },
    // 重置
    handleReset() {
      console.log('重置')
    },
    // 入库列表
    inWare() {
      wareAll().then(res => {
        this.tableData = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    // 重置表单
    resetTemp() {
      this.temp = {
        num: '', // 采购单号
        supplyId: '', // 供应商id
        productId: '', // 产品id
        count: 0 // 采购数量
      }
    },
    // 点击新增
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    // 新增采购
    createData() {
      console.log('新增采购')
      this.dialogFormVisible = false
      // this.$refs['dataForm'].validate((valid) => {
      //   if (valid) {
      //     this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
      //     this.temp.author = 'vue-element-admin'
      //     createArticle(this.temp).then(() => {
      //       this.list.unshift(this.temp)
      //       this.dialogFormVisible = false
      //       this.$notify({
      //         title: 'Success',
      //         message: 'Created Successfully',
      //         type: 'success',
      //         duration: 2000
      //       })
      //     })
      //   }
      // })
    },
    // 编辑行
    handleEdit(index, row) {},
    // 删除
    handleDelete(index, row) {},
    // 付款
    handlePay(index, row) {},
    // 分页事件
    getList() {
      console.log('分页事件')
      // fetchList(this.listQuery).then(response => {
      //   this.list = response.data.items
      //   this.total = response.data.total
      //
      //   // Just to simulate the time of the request
      //   setTimeout(() => {
      //     this.listLoading = false
      //   }, 1.5 * 1000)
      // })
    }
  }
}
</script>
